unit UServiceThreads;

interface

uses
  Classes, UDAMOGrau1, UDAMOGrau2;

type
  ThreadPush = class (TThread)
  private
    FTimeOut: Cardinal;
  protected
    procedure Execute; override;
  public
    constructor Create;
    function WaitFor: LongWord;
    property TimeOut: Cardinal read FTimeOut write FTimeOut;
    property Terminated;
  end;

  TThreadGrau1 = class(ThreadPush)
  private
    FDAMOGrau1: TDAMOGrau1;
  protected
    procedure Execute; override;
  public
    constructor Create;
    destructor Destroy; override;
  end;

  TThreadGrau2 = class(ThreadPush)
  private
    FDAMOGrau2: TDAMOGrau2;
  protected
    procedure Execute; override;
  public
    constructor Create;
    destructor Destroy; override;
  end;

implementation

uses
  SvcMgr, SysUtils, Windows;

{ FThreadGrau1 }

constructor TThreadGrau1.Create;
begin
  inherited;
  FDAMOGrau1 := TDAMOGrau1.Create(Application);
end;

destructor TThreadGrau1.Destroy;
begin
  FDAMOGrau1.Free;
  inherited;
end;

procedure TThreadGrau1.Execute;
begin
  inherited;
  try
    FDAMOGrau1.ProcessarEmails;
  finally
    Terminate;
  end;
end;

{ FThreadGrau2 }

constructor TThreadGrau2.Create;
begin
  inherited;
  FDAMOGrau2 := TDAMOGrau2.Create(nil);
end;

destructor TThreadGrau2.Destroy;
begin
  FDAMOGrau2.Free;
  inherited;
end;

procedure TThreadGrau2.Execute;
begin
  inherited;
  try
    FDAMOGrau2.ProcessarEmails;
  finally
    Terminate;
  end;
end;

{ ThreadPush }

constructor ThreadPush.Create;
begin
  FTimeOut := 60;
  FreeOnTerminate := False;
  inherited Create(True);
end;

procedure ThreadPush.Execute;
begin
  inherited;
end;

function ThreadPush.WaitFor: LongWord;
begin
  WaitForSingleObject(Handle, INFINITE);
  CheckThreadError(GetExitCodeThread(Handle, Result));
end;

end.
